<?php
//POST请求
class POSTNEW{

	var $ch=null;
	var $cookiefile="";
	var $HTTP_USER_AGENT="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)";
	var $refer="";
	var $call_c=0;


	function httpGet($url,$data){
		return $this->httpRequest("GET",$url,$data);
	}
	function httpPost($url,$data){
		return $this->httpRequest("POST",$url,$data);
	}

	function httpRequest($method, $url, $vars) {
		$urlinfo = parse_url($url);
		$this->cookiefile = $urlinfo['host'];
		if($this->call_c==0){
			if($this->ch!=null){
				curl_close($this->ch);
				unset($this->ch);
			}
			$this->ch=curl_init();
		}
		$ch = $this->ch; //
		////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////
		//if($this->header!=null){
		//	//if($this->debug)print "DEBUG: CURLOPT_HTTPHEADER!!";//print_r($this->header);
			//	curl_setopt($ch,CURLOPT_HTTPHEADER,$this->header);//TODO this is a array...
			//	$this->header=null;
			//}else{
			curl_setopt($this->ch, CURLOPT_USERAGENT, $this->HTTP_USER_AGENT);
			//$HTTP_USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
			//if($HTTP_USER_AGENT!="") curl_setopt($ch, CURLOPT_USERAGENT, $HTTP_USER_AGENT);
			/*if(@$this->_flag_memcache){
			 $cookie_mem=$this->loadSessionVar("COOKIE");
			 $cookie=unserialize($cookie_mem);
			 if($this->debug) quicklog("DEBUG-netCommon","Load Mem Cookie:$cookie_mem  -- $url");
			 if ( count($cookie) > 0 ) {
			 //$cookie_str = "Cookie:\t";
			 foreach ( $cookie as $cookieKey => $cookieVal ) { $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; "; }
			 }
			 curl_setopt($ch, CURLOPT_COOKIESESSION, false);
			 if($cookie_str != ""){
			 if($this->debug) quicklog("DEBUG-netCommon","Send:[$cookie_str]  -- $url");
			 curl_setopt($ch, CURLOPT_COOKIE, $cookie_str);
			 }
			 }else if($this->cookiefile!=""){*/
			curl_setopt($ch, CURLOPT_COOKIESESSION, false);
			curl_setopt($ch, CURLOPT_COOKIEJAR,_TMP_.'cookie/'.$this->cookiefile .'.txt');
			curl_setopt($ch, CURLOPT_COOKIEFILE,_TMP_.'cookie/'.$this->cookiefile .'.txt');
			//}
			if($this->refer!=""){
				curl_setopt($ch, CURLOPT_REFERER, $this->refer);
				$this->refer="";//clear
			}else{
				curl_setopt($ch, CURLOPT_AUTOREFERER, true);
			}
			//}
			//TODO
			//CURLOPT_HTTPPROXYTUNNEL
			//CURLOPT_PROXYAUTH,CURLAUTH_BASIC and CURLAUTH_NTLM
			//CURLOPT_PROXY
			//CURLOPT_PROXYPORT
			//CURLOPT_PROXYTYPE Either CURLPROXY_HTTP (default) or CURLPROXY_SOCKS5.
			//CURLOPT_PROXYUSERPWD;A username and password formatted as "[username]:[password]" to use for the connection to the proxy.
			if ($method == "POST") {
				curl_setopt($ch, CURLOPT_HTTPGET, false);
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
			}else{
				curl_setopt($ch, CURLOPT_POST, false);
				curl_setopt($ch, CURLOPT_POSTFIELDS, false);
				curl_setopt($ch, CURLOPT_HTTPGET, true);
			}
			////////////////////////////////////////////////////////////
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($ch, CURLOPT_TIMEOUT_MS,10000);
			//curl_setopt($ch, CURLOPT_VERBOSE, @$this->verbose);
			////////////////////////////////////////////////////////////
			curl_setopt($ch, CURLOPT_URL, $url);
			try{
				$data = curl_exec($ch);
				if(TK_DEBUG)Log::Write($url."--".$vars."--".var_export($data,true),"postnew.txt");
				$info=curl_getinfo($ch);
				if(!$info['http_code']){
					$curl_error=curl_error($ch);
					Log::Write($curl_error,"posterror.txt");
					return $curl_error;
				}else if($info['http_code']==200){
					if ($data) {
						if (@$this->callback)
						{
							$callback = $this->callback;
							$this->callback = false;
							call_user_func($callback, $data);
						} else {
							$str=array(
							0=>"号码不正确",
							1=>"注册成功",
							2=>"密令验证失败",
							3=>"用户名不能为空",
							4=>"呢称不能为空",
							5=>"密码不能为空",
							6=>"邮箱不能为空",
							7=>"邮箱格式不正确",
							8=>"该号码已被注册",
							9=>"该邮箱已注册过会员",
							10=>"昵称已被使用",
							11=>"设置密码成功",
							12=>"指令不正确",
							13=>"会员验证成功",
							14=>"找回支付密码成功",
							22=>"未知错误",
							51=>"网站未开通短信注册",
							55=>"网站未开通"
							);
							if(stripos($data, " ")){
								$body=explode(" ",trim(strip_tags($data)));
								if(TK_DEBUG)Log::Write($body[1],"UrlResultOthor.txt");
								$body = $body[0];
							}else{
								$body=trim(strip_tags($data));
							}
								
							//Log::Write($data,"postdata.txt");
							if(isset($str[$body])){
								return $str[$body];
							}else{
								if(TK_DEBUG)@Log::Write($data,"UrlResultError");
								return "$url 返回结果错误,请检查接口是否有问题";
							}
							/*if(@is_numeric($body) &&($cid=@intval($body))<count($str)){
							 return $str[$cid];
							 }else if(!@is_numeric($body)){
							 return "注册接口版本太低!请更新!";
							 }else{
							 return "$url 返回结果错误";
							 }*/
						}
					} else {
						$curl_error=curl_error($ch);
						return $curl_error;
					}
				}else{
					if(TK_DEBUG)Log::Write($data,"header-{$info['http_code']}.txt");
			  if(L($info['http_code'])==$info['http_code']){
			  	return "调用注册接口时出现".$info['http_code']."错误.耗时:".$info['total_time'];
			  }else{
			  	return "调用注册接口时出现".($info['http_code'])."错误.耗时:".$info['total_time'];
			  }
				}
			}catch(Exception $e){
				return $e->getMessage();
			}
	}
	function __destruct(){
		if ($this->ch!=null)curl_close($this->ch);
	}
}